package common.util;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.Cookie;

import com.swsp.config.Global;
import com.swsp.util.LogManager;

import common.util.crypto.Crypto;

public final class CookieManager {
	
	private static HttpServletResponse response;
	private static HttpServletRequest request;
//	private static CookieManager Cookies = new CookieManager();
	
	private CookieManager(HttpServletRequest req , HttpServletResponse res) {
		setReq(req);
		setRes(res);
	}
	
	public static CookieManager getInstance(HttpServletRequest req , HttpServletResponse res) {
		CookieManager Cookies = new CookieManager(req , res);
		return Cookies;
	}
	
	private void setReq(HttpServletRequest req) {
		request = req;
	}

	private void setRes(HttpServletResponse res) {
		response = res;
	}
	public void addCookie(String key , String value) {
		addCookie(key ,value , -1);
	}
	public void addCookie(String key , String value , int time) {
		
		Cookie tmp;
		try {
			String tmpValue = Crypto.encrypt(value);
			tmp = new Cookie(key, URLEncoder.encode(tmpValue));
			tmp.setPath("/");
			tmp.setMaxAge(time);
			tmp.setSecure(false);
			
			response.addCookie(tmp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void addCookie(String key , String value , String url) {
		
		Cookie tmp;
		try {
			String tmpValue = Crypto.encrypt(value);
			tmp = new Cookie(key, URLEncoder.encode(tmpValue));
			tmp.setPath("/");
			tmp.setDomain(url);
			tmp.setMaxAge(-1);
			tmp.setSecure(false);
			
			response.addCookie(tmp);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	public String getCookie(String key) {
		if(key == null) {
			return "";
		}
		if(key.equals("")) {
			return "";
		}
		Cookie[] tmp = request.getCookies();
		if(tmp != null) {
			String ckeVal = "";
			try {
				for(int i = 0 ; i < tmp.length ; i++){
					if(tmp[i].getName().equals(key)) {
						ckeVal = URLDecoder.decode(tmp[i].getValue(), "EUC-KR").replace(" ", "+");
	//					ckeVal = URLDecoder.decode(tmp[i].getValue().replace("%2B", "+"), "EUC-KR");
						String retVal = "";
						System.out.println( "ckeVal : "+ckeVal );
						retVal = Crypto.decrypt( ckeVal );
						return URLDecoder.decode(retVal, "EUC-KR");
					}
				}
			} catch (Exception e) {
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "### User Infomation ##################################");
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "# Cookies " );
				for (int i=0; i < tmp.length; i++) {
					String name = tmp[i].getName();
					String value = tmp[i].getValue();
				  	Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "#  "+ i +". " + name + " : " + value );
				}
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "# referer : " + request.getHeader("referer"));
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "# REMOTE_ADDR : " + request.getRemoteHost());
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "# REMOTE_USER : " + request.getRemoteUser());
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "# CharacterEncoding : " + request.getCharacterEncoding());
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "# User-Agent : " + request.getHeader("User-Agent"));
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "######################################################");
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "# Error Cookie ["+ckeVal+"] : [ " + e.toString() + " ]");
				e.printStackTrace();
			} 
		}
		return "";
	}
	
	public String getCookie2(String key) {
		if(key == null) {
			return "";
		}
		if(key.equals("")) {
			return "";
		}
		Cookie[] tmp = request.getCookies();
		if(tmp != null) {
			String ckeVal = "";
			try {
				for(int i = 0 ; i < tmp.length ; i++){
					if(tmp[i].getName().equals(key)) {
						ckeVal = URLDecoder.decode(tmp[i].getValue(), "EUC-KR").replace(" ", "+");
						String retVal = "";
						return URLDecoder.decode(ckeVal, "EUC-KR");
					}
				}
			} catch (Exception e) {
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "### User Infomation ##################################");
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "# Cookies " );
				for (int i=0; i < tmp.length; i++) {
					String name = tmp[i].getName();
					String value = tmp[i].getValue();
					Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "#  "+ i +". " + name + " : " + value );
				}
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "# referer : " + request.getHeader("referer"));
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "# REMOTE_ADDR : " + request.getRemoteHost());
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "# REMOTE_USER : " + request.getRemoteUser());
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "# CharacterEncoding : " + request.getCharacterEncoding());
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "# User-Agent : " + request.getHeader("User-Agent"));
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "######################################################");
				Global.LOG_MANAGER.writeLog(Global.LOG_MANAGER.ERROR, "# Error Cookie ["+ckeVal+"] : [ " + e.toString() + " ]");
				e.printStackTrace();
			} 
		}
		return "";
	}
	
	public void deleteCookie(String key) {
		
		Cookie[] tmp = request.getCookies();
		if(tmp != null) {
			try {
				for(int i = 0 ; i < tmp.length ; i++) {
					if(tmp[i].getName().equals(key)) {
						tmp[i].setMaxAge(0);
						response.addCookie(tmp[i]);
					}
				}
			}
			catch( Exception e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public void delAllCookie() {
		Cookie[] tmp = request.getCookies();
		if(tmp != null) {
			
			for(int i = 0 ; i < tmp.length ; i++){
				// tmp[i].setMaxAge(1);
				tmp[i].setMaxAge(0);
				
				response.addCookie(tmp[i]);
			}
			
		}
	}
	
}
